package com.glut.campusrepair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glut.campusrepair.dto.RepairTasksShortdto;
import com.glut.campusrepair.service.IRepairTasksService;
import com.glut.campusrepair.entity.RepairTasks;
import com.glut.campusrepair.mapper.RepairTasksMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维修进度表 服务实现类
 *
 * @author sanminzhi
 * @since 2024-07-19 11:26
 */
@Service
public class RepairTasksServiceImpl extends ServiceImpl<RepairTasksMapper, RepairTasks> implements IRepairTasksService {


    @Autowired
    RepairTasksMapper repairTasksMapper;

    RepairTasksShortdto repairTasksShortdto;

    /**
     * 通过维修人员id查询所有简洁版的待接受的任务，再通过其中的故障报修id查找出相应的报修的故障信息
     * */
    @Override
    public List<RepairTasksShortdto> selectAssociation(Integer repairPersonnelId) {
        return repairTasksMapper.selectAssociation(repairPersonnelId);
    }


    /**
     * 根据维修人员Id查询所有任务
     * */
    @Override
    public List<RepairTasksShortdto> selectTasksById(RepairTasksShortdto repairTasksShortdto) {
        Integer repairPersonnelId = repairTasksShortdto.getRepairPersonnelId();
        String status = repairTasksShortdto.getStatus();

        return repairTasksMapper.selectTasksById(repairPersonnelId,status);
    }

    /**
     * 接受任务
     * 点击接受任务时，将repair_task表中的任务状态参数（status）值为'已接受',并将接受任务的时间赋值给acceptance_time属性
     * */
    @Override
    public boolean updateById(Integer taskId) {
        repairTasksShortdto = new RepairTasksShortdto();
        repairTasksShortdto.setStatus("正在处理");
        repairTasksShortdto.setAcceptanceTime(new Date());
        repairTasksShortdto.setTaskId(taskId);

        System.out.println(new Date());

        int num = repairTasksMapper.updateById(repairTasksShortdto);

        return num == 1 ? true : false;
    }


    @Override
    public RepairTasks setRepairTasks(Integer id){
        QueryWrapper<RepairTasks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("repair_personnel_id", id)
                .eq("status", "正在维修");
        RepairTasks repairTask = getOne(queryWrapper);
        return repairTask;
    }




   /* @Override
    @Transactional
    public void upRepairTasks(Integer id) {
        // 根据维修人员id查询其负责的维修任务列表
        QueryWrapper<RepairTasks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("repair_personnel_id", id)
                    .eq("status", "正在维修");
        RepairTasks repairTask = getOne(queryWrapper);

        if (repairTask != null) {
            repairTask.setStatus("维修完毕");
            repairTask.setCompletionTime(new Date());
            // 更新数据库中的记录
            updateById(repairTask);
        }

    }*/

    @Override
    public boolean upCompletionPhoto(Integer taskId, String completionPhoto) {
        return repairTasksMapper.upCompletionPhoto(taskId, completionPhoto) > 0;
    }


    @Override
    public String getCompletionPhoto(Integer taskId){
        QueryWrapper<RepairTasks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        RepairTasks repairTask = getOne(queryWrapper);
        String completionPhoto = "图片路径为空";
        if (repairTask != null && repairTask.getCompletionPhoto()!=null){
            completionPhoto = repairTask.getCompletionPhoto();
        }
        return completionPhoto;
    }
}
